package com.jinly.ryder.manage.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.account.service.UserAuthService;
import com.jinly.ryder.account.service.model.UserAuth;
import com.jinly.ryder.account.web.utils.AuthUtil;
import com.jinly.ryder.common.auth.LoginContent;
import com.jinly.ryder.common.auth.annotation.Auth;
import com.jinly.ryder.common.auth.annotation.AuthParam;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import com.jinly.ryder.manage.service.AppInfoService;
import com.jinly.ryder.manage.service.model.AppInfo;
import com.jinly.ryder.manage.web.mapping.AppInfoVOMapping;
import com.jinly.ryder.manage.web.vo.AppInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("manage/app")
@RequiredArgsConstructor
public class AppInfoController {

    private final AppInfoService appInfoService;
    private final AppInfoVOMapping appInfoVOMapping;
    private final UserAuthService userAuthService;

    @GetMapping("list")
    public List<AppInfoVO> appList() {
        List<AppInfoVO> allList = appInfoVOMapping.toVoList(appInfoService.allList());
        boolean isSuperAdmin = AuthUtil.isSuperAdmin();
        Map<String, AuthRole> appRoleMap = new HashMap<>();
        if (!isSuperAdmin) {
            List<UserAuth> userAuths = userAuthService.queryUserAuthListByUserId(LoginContent.getUserId(), AuthType.APP);
            appRoleMap = userAuths.stream().collect(Collectors.toMap(UserAuth::getAuthData, UserAuth::getAuthRole));
        }
        List<AppInfoVO> list = new ArrayList<>();
        for (AppInfoVO appInfoVO : allList) {
            if (isSuperAdmin || appRoleMap.containsKey(appInfoVO.getAppName())) {
                if (isSuperAdmin || appRoleMap.get(appInfoVO.getAppName()) == AuthRole.ADMIN) {
                    appInfoVO.setAdminUser(true);
                }
                list.add(appInfoVO);
            }
        }
        return list;
    }

    @GetMapping("{appName}")
    @Auth(type = AuthType.APP, role = AuthRole.TESTER, value = "#{appName}")
    public AppInfoVO getAppInfo(@PathVariable @AuthParam("appName") String appName) {
        AppInfoVO appInfoVO = appInfoVOMapping.toVo(appInfoService.getAppInfo(appName));
        if (appInfoVO != null) {
            if (AuthUtil.isSuperAdmin() || AuthUtil.hasAuth(AuthType.MANAGE, "app", AuthRole.ADMIN)
                    || AuthUtil.hasAuth(AuthType.APP, appName, AuthRole.ADMIN)) {
                appInfoVO.setAdminUser(true);
            }
        }
        return appInfoVO;
    }

    @GetMapping("page")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public Page<AppInfoVO> appPage(Long current, Long size, AppInfoVO query) {
        Page<AppInfo> page = appInfoService.queryAppPage(new Page(current, size), appInfoVOMapping.toModel(query));
        Page<AppInfoVO> pageVO = appInfoVOMapping.toVOPage(page);
        pageVO.getRecords().forEach(a -> a.setAdminUser(true));
        return pageVO;
    }

    @PostMapping("saveApp")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public void saveAppInfo(@RequestBody @AuthParam("appName") AppInfoVO appInfoVO) {
        appInfoService.save(appInfoVOMapping.toModel(appInfoVO));
    }

    @DeleteMapping("{appName}")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public void deleteAppInfo(@PathVariable @AuthParam("appName") String appName) {
        appInfoService.deleteApp(appName);
    }
}
